﻿using System;
using System.Windows.Forms;

namespace DataCenter.Component.Processor
{
    public partial class InterpolateProcessorEditForm : Form
    {
        private InterpolateProcessorEditForm()
        {
            InitializeComponent();
        }
        public InterpolateProcessorEditForm(InterpolateProcessorEntity entity, bool isModify)
            :this()
        {
            this.IsModify = isModify;
            this.Entity = entity;
        }
        private bool IsModify;
        private InterpolateProcessorEntity Entity;

        private void InterpolateProcessorEditForm_Load(object sender, EventArgs e)
        {
            if (!IsModify)
                cboInterpolateType.SelectedIndex = 0;
            else
            {
                txtComponentName.Text = Entity.ComponentName;
                txtForwardSearchMinutes.Text = Entity.ForwardSearchMinutes.ToString();
                txtBackwardSearchMinutes.Text = Entity.BackwardSearchMinutes.ToString();                
                if (Entity.InterpolateType == InterpolateTypes.Linear) 
                    cboInterpolateType.SelectedIndex = 0;
                else cboInterpolateType.SelectedIndex = 1;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!Check()) return;

            Entity.ComponentName = txtComponentName.Text.Trim();
            Entity.IsRealValueAsDefault = chkUseDefault.Checked;
            Entity.InterpolateType = (InterpolateTypes)(cboInterpolateType.SelectedIndex + 1);
            Entity.ForwardSearchMinutes = Convert.ToInt32(txtForwardSearchMinutes.Text);
            Entity.BackwardSearchMinutes = Convert.ToInt32(txtBackwardSearchMinutes.Text);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private bool Check()
        {
            bool checkResult = true;
            string errmsg = "";
            TextBox ctrl = null;
            int iValue;

            if (string.IsNullOrEmpty(txtComponentName.Text))
            {
                errmsg = "组件名称不能为空";
                ctrl = txtComponentName;
                checkResult = false;
                goto ShowError;
            }
            if (string.IsNullOrEmpty(txtForwardSearchMinutes.Text))
            {
                errmsg = "向前搜索时间不能为空";
                ctrl = txtForwardSearchMinutes;
                checkResult = false;
                goto ShowError;
            }
            if (!int.TryParse(txtForwardSearchMinutes.Text, out iValue))
            {
                errmsg = "向前搜索时间为有效的整数";
                ctrl = txtForwardSearchMinutes;
                checkResult = false;
                goto ShowError;
            }
            if (iValue < 0)
            {
                errmsg = "向前搜索时间必须大于等于0";
                ctrl = txtForwardSearchMinutes;
                checkResult = false;
                goto ShowError;
            }
            if (string.IsNullOrEmpty(txtBackwardSearchMinutes.Text))
            {
                errmsg = "向后搜索时间不能为空";
                ctrl = txtBackwardSearchMinutes;
                checkResult = false;
                goto ShowError;
            }
            if (!int.TryParse(txtBackwardSearchMinutes.Text, out iValue))
            {
                errmsg = "向后搜索时间为有效的整数";
                ctrl = txtBackwardSearchMinutes;
                checkResult = false;
                goto ShowError;
            }
            if (iValue < 0)
            {
                errmsg = "向后搜索时间必须大于等于0";
                ctrl = txtBackwardSearchMinutes;
                checkResult = false;
                goto ShowError;
            }

            return true;

        ShowError:
            if (!checkResult)
            {
                MessageBox.Show(errmsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                if (ctrl != null) ctrl.Focus();
            }
            return false;
        }
    }
}
